package com.winit.openapi.tms.builder;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.tms.constant.BookingConstant;
import com.winit.tms.spi.booking.command.WarehouseInBookingOrderQueryCommand;
import com.winit.tms.spi.booking.vo.BookingOrderCancelValidateVo;
import com.winit.tms.spi.booking.vo.BookingOrderDocumentDetailVo;
import com.winit.tms.spi.booking.vo.BookingOrderValueAddedServiceVo;
import com.winit.tms.spi.booking.vo.WarehouseInBookingOperateLogVo;
import com.winit.tms.spi.booking.vo.WarehouseInBookingOrderDetailsVo;
import com.winit.tms.spi.booking.vo.WarehouseInBookingOrderVo;
import com.winit.tms.spi.booking.vo.WarehouseinRuleUnloadTypeVo;

public class BookingOrderBuilder {

    public static Map<String, Object> buildReturnBookingOrderCancelValidateVo(BookingOrderCancelValidateVo vo) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (null == vo) {
            return map;
        }
        map.put("isOvertime", vo.getIsOvertime());
        map.put("advanceDateQty", vo.getAdvanceDateQty());
        map.put("amount", vo.getAmount());
        return map;
    }

    public static WarehouseInBookingOrderQueryCommand buildQueryCommand(JSONObject json) {
        WarehouseInBookingOrderQueryCommand command = new WarehouseInBookingOrderQueryCommand();
        if (json.containsKey("bookingType")) {
            command.setBookingType(json.getString("bookingType"));
        }
        if (json.containsKey("bookingCode")) {
            command.setBookingCode(json.getString("bookingCode"));
        }
        if (json.containsKey("orderNo")) {
            command.setOrderNo(json.getString("orderNo"));
        }
        if (json.containsKey("bookingNo")) {
            command.setBookingNo(json.getString("bookingNo"));
        }
        if (json.containsKey("logisticsNo")) {
            command.setLogisticsNo(json.getString("logisticsNo"));
        }
        if (json.containsKey("containerNo")) {
            command.setContainerNo(json.getString("containerNo"));
        }
        if (json.containsKey("trackingNo")) {
            command.setTrackingNo(json.getString("trackingNo"));
        }
        if (json.containsKey("warehouseCode")) {
            command.setWarehouseCode(json.getString("warehouseCode"));
        }
        if (json.containsKey("deliveryWayType")) {
            command.setDeliveryWayType(json.getString("deliveryWayType"));
        }
        if (json.containsKey("docType")) {
            command.setDocType(json.getString("docType"));
        }
        if (json.containsKey("status")) {
            command.setStatus(json.getString("status"));
        }
        if (json.containsKey("bookingDateFrom")) {
            command.setBookingDateFrom(json.getLong("bookingDateFrom"));
        }
        if (json.containsKey("orderDateFrom")) {
            command.setOrderDateFrom(json.getDate("orderDateFrom"));
        }
        if (json.containsKey("orderDateTo")) {
            command.setOrderDateTo(json.getDate("orderDateTo"));
        }
        if(json.containsKey("sendWarehouseNo")){
            command.setSendWarehouseNo(json.getString("sendWarehouseNo"));
        }
        return command;
    }

    public static WarehouseInBookingOrderVo buildBookingOrderVo(JSONObject json, String source) {
        WarehouseInBookingOrderVo orderVo = new WarehouseInBookingOrderVo();
        if (json.containsKey("id")) {
            orderVo.setId(json.getLong("id"));
        }
        if (json.containsKey("bookingNo")) {
            orderVo.setBookingNo(json.getString("bookingNo"));
        }
        if (json.containsKey("warehouseCode")) {
            orderVo.setWarehouseCode(json.getString("warehouseCode"));
        }
        if (json.containsKey("warehouseName")) {
            orderVo.setWarehouseName(json.getString("warehouseName"));
        }
        if (json.containsKey("volume")) {
            orderVo.setVolume(json.getBigDecimal("volume"));
        }
        if (json.containsKey("weight")) {
            orderVo.setWeight(json.getBigDecimal("weight"));
        }
        if (json.containsKey("packageQty")) {
            orderVo.setPackageQty(json.getInteger("packageQty"));
        }
        if (json.containsKey("itemQty")) {
            orderVo.setItemQty(json.getInteger("itemQty"));
        }
        if (json.containsKey("merchandiseQty")) {
            orderVo.setMerchandiseQty(json.getInteger("merchandiseQty"));
        }
        if (json.containsKey("bookingType")) {
            orderVo.setBookingType(json.getString("bookingType"));
        }
        if (json.containsKey("docType")) {
            orderVo.setDocType(json.getString("docType"));
        }
        if (json.containsKey("status")) {
            orderVo.setStatus(json.getString("status"));
        }
        if (json.containsKey("deliveryWayType")) {
            orderVo.setDeliveryWayType(json.getString("deliveryWayType"));
        }
        if (json.containsKey("bookingDate")) {
            orderVo.setBookingDate(json.getLong("bookingDate"));
        }
        if (json.containsKey("bookingDateTime")) {
            orderVo.setBookingDateTime(json.getString("bookingDateTime"));
        }
        if (json.containsKey("containerNo")) {
            orderVo.setContainerNo(json.getString("containerNo"));
        }
        if (json.containsKey("trackingNo")) {
            orderVo.setTrackingNo(json.getString("trackingNo"));
        }
        if (json.containsKey("sealNo")) {
            orderVo.setSealNo(json.getString("sealNo"));
        }
        if (json.containsKey("cabinetType")) {
            orderVo.setCabinetType(json.getString("cabinetType"));
        }
        if (json.containsKey("isPallet")) {
            orderVo.setIsPallet(json.getString("isPallet"));
        }
        if (json.containsKey("palletQty")) {
            orderVo.setPalletQty(json.getInteger("palletQty"));
        }
        if (json.containsKey("contactPerson")) {
            orderVo.setContactPerson(json.getString("contactPerson"));
        }
        if (json.containsKey("contactNumper")) {
            orderVo.setContactNumper(json.getString("contactNumper"));
        }
        if (json.containsKey("email")) {
            orderVo.setEmail(json.getString("email"));
        }
        if (json.containsKey("isNightStay")) {
            orderVo.setIsNightStay(json.getString("isNightStay"));
        }
        if (json.containsKey("warehouseinPlanId")) {
            orderVo.setWarehouseinPlanId(json.getLong("warehouseinPlanId"));
        }
        if (json.containsKey("bookingTime")) {
            orderVo.setBookingTime(json.getInteger("bookingTime"));
        }
        if (json.containsKey("arriveWarehouseDate")) {
            orderVo.setArriveWarehouseDate(json.getDate("arriveWarehouseDate"));
        }
        if (json.containsKey("supplierCode")) {
            orderVo.setSupplierCode(json.getString("supplierCode"));
        }
        if (json.containsKey("supplierName")) {
            orderVo.setSupplierName(json.getString("supplierName"));
        }
        if (json.containsKey("supplierServiceCode")) {
            orderVo.setSupplierServiceCode(json.getString("supplierServiceCode"));
        }
        if (json.containsKey("supplierServiceName")) {
            orderVo.setSupplierServiceName(json.getString("supplierServiceName"));
        }
        if (json.containsKey("bookingSuccessDate")) {
            orderVo.setBookingSuccessDate(json.getLong("bookingSuccessDate"));
        }
        if (json.containsKey("referenceNo")) {
            orderVo.setReferenceNo(json.getString("referenceNo"));
        }
        if (json.containsKey("unloadType")) {
            orderVo.setUnloadType(json.getString("unloadType"));
        }
        orderVo.setSource(source);
        // 增值预约
        if (BookingConstant.BookingOrderType.ValueAdded.getType().equals(orderVo.getBookingType())) {
            BookingOrderValueAddedServiceVo serviceVo = BookingOrderValueAddedServiceBuilder.buildBookingValueAddedService(json);

            if (null != serviceVo) {
                orderVo.setBookingOrderValueAddedServiceVo(serviceVo);
            }
        }

        BigDecimal totalVolume = BigDecimal.ZERO;

        List<BookingOrderDocumentDetailVo> documentDetailList = BookingOrderDocumentDetailBuilder.buildDocumentDetailList(json);
        if (CollectionUtils.isNotEmpty(documentDetailList)) {
            orderVo.setBookingOrderDocumentDetailVoList(documentDetailList);
        }
        orderVo.setVolume(totalVolume);
        return orderVo;
    }

    public static Map<String, Object> buildReturnBookingOrderInfoForAPI(WarehouseInBookingOrderDetailsVo vo) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (null == vo) {
            return map;
        }
        map.put("id", vo.getId());
        map.put("bookingNo", vo.getBookingNo());
        map.put("bookingCode", vo.getBookingCode());
        map.put("warehouseCode", vo.getWarehouseCode());
        map.put("warehouseName", vo.getWarehouseName());
        map.put("customerCode", vo.getCustomerCode());
        map.put("customerName", vo.getCustomerName());
        map.put("volume", vo.getVolume());
        map.put("weight", vo.getWeight());
        map.put("packageQty", vo.getPackageQty());
        map.put("itemQty", vo.getItemQty());
        map.put("merchandiseQty", vo.getMerchandiseQty());
        map.put("bookingType", vo.getBookingType());
        map.put("docType", vo.getDocType());
        map.put("status", vo.getStatus());
        map.put("deliveryWayType", vo.getDeliveryWayType());
        map.put("bookingDate", vo.getBookingDate());
        map.put("bookingDateTime", vo.getBookingDateTime());
        map.put("containerNo", vo.getContainerNo());
        map.put("trackingNo", vo.getTrackingNo());
        map.put("sealNo", vo.getSealNo());
        map.put("cabinetType", vo.getCabinetType());
        map.put("isPallet", vo.getIsPallet());
        map.put("palletQty", vo.getPalletQty());
        map.put("contactPerson", vo.getContactPerson());
        map.put("contactNumper", vo.getContactNumper());
        map.put("email", vo.getEmail());
        map.put("isNightStay", vo.getIsNightStay());
        map.put("warehouseinPlanId", vo.getWarehouseinPlanId());
        map.put("bookingTime", vo.getBookingTime());
        map.put("arriveWarehouseDate", vo.getArriveWarehouseDate());
        map.put("supplierCode", vo.getSupplierCode());
        map.put("supplierName", vo.getSupplierName());
        map.put("supplierServiceCode", vo.getSupplierCode());
        map.put("supplierServiceName", vo.getSupplierServiceName());
        map.put("bookingSuccessDate", vo.getBookingSuccessDate());
        map.put("referenceNo", vo.getReferenceNo());
        map.put("createdby", vo.getCreatedby());
        map.put("created", vo.getCreated());
        map.put("updatedby", vo.getUpdatedby());
        map.put("updated", vo.getUpdated());
        map.put("unloadType", vo.getUnloadType());
        map.put("startPortCode", vo.getStartPortCode());
        map.put("startPortName", vo.getStartPortName());
        map.put("endPortCode", vo.getEndPortCode());
        map.put("endPortName", vo.getEndPortName());
        map.put("transportType", vo.getTransportType());

        List<Map<String, Object>> documentDetailsList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> operateLogList = new ArrayList<Map<String, Object>>();
        Map<String, Object> valueAddedServiceMap = new HashMap<String, Object>();

        if (CollectionUtils.isNotEmpty(vo.getDocumentDetailVoList())) {
            for (BookingOrderDocumentDetailVo documentDetailVo : vo.getDocumentDetailVoList()) {
                documentDetailsList.add(BookingOrderDocumentDetailBuilder.buildReturnDocumentDetail(documentDetailVo));
            }
        }

        if (CollectionUtils.isNotEmpty(vo.getOperateLogList())) {
            for (WarehouseInBookingOperateLogVo operateLogVo : vo.getOperateLogList()) {
                operateLogList.add(BookingOperateLogBuilder.buildReturnOperateLog(operateLogVo));
            }
        }

        if (null != vo.getBookingOrderValueAddedServiceVo()) {
            valueAddedServiceMap = BookingOrderValueAddedServiceBuilder.buildBookingValueAddedService(vo.getBookingOrderValueAddedServiceVo());
        }
        map.put("documentDetailsList", documentDetailsList);
        map.put("operateLogList", operateLogList);
        map.put("warehouseInBookingOrderValueAddedService", valueAddedServiceMap);
        return map;
    }

    public static List<Map<String, Object>> buildReturnWarehouseInBookingOrderList(List<WarehouseInBookingOrderVo> voList) {
        List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
        if (org.springframework.util.CollectionUtils.isEmpty(voList)) {
            return returnList;
        }
        for (WarehouseInBookingOrderVo vo : voList) {
            returnList.add(buildReturnWarehouseInBookingOrder(vo));
        }
        return returnList;
    }

    public static Map<String, Object> buildReturnWarehouseInBookingOrder(WarehouseInBookingOrderVo vo) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (null == vo) {
            return map;
        }
        map.put("id", vo.getId());
        map.put("bookingNo", vo.getBookingNo());
        map.put("bookingCode", vo.getBookingCode());
        map.put("warehouseCode", vo.getWarehouseCode());
        map.put("warehouseName", vo.getWarehouseName());
        map.put("customerCode", vo.getCustomerCode());
        map.put("customerName", vo.getCustomerName());
        map.put("volume", vo.getVolume());
        map.put("weight", vo.getWeight());
        map.put("packageQty", vo.getPackageQty());
        map.put("itemQty", vo.getItemQty());
        map.put("merchandiseQty", vo.getMerchandiseQty());
        map.put("bookingType", vo.getBookingType());
        map.put("docType", vo.getDocType());
        map.put("status", vo.getStatus());
        map.put("deliveryWayType", vo.getDeliveryWayType());
        map.put("bookingDate", vo.getBookingDate());
        map.put("bookingDateTime", vo.getBookingDateTime());
        map.put("containerNo", vo.getContainerNo());
        map.put("trackingNo", vo.getTrackingNo());
        map.put("sealNo", vo.getSealNo());
        map.put("cabinetType", vo.getCabinetType());
        map.put("isPallet", vo.getIsPallet());
        map.put("palletQty", vo.getPalletQty());
        map.put("contactPerson", vo.getContactPerson());
        map.put("contactNumper", vo.getContactNumper());
        map.put("email", vo.getEmail());
        map.put("isNightStay", vo.getIsNightStay());
        map.put("warehouseinPlanId", vo.getWarehouseinPlanId());
        map.put("bookingTime", vo.getBookingTime());
        map.put("arriveWarehouseDate", vo.getArriveWarehouseDate());
        map.put("supplierCode", vo.getSupplierCode());
        map.put("supplierName", vo.getSupplierName());
        map.put("supplierServiceCode", vo.getSupplierCode());
        map.put("supplierServiceName", vo.getSupplierServiceName());
        map.put("bookingSuccessDate", vo.getBookingSuccessDate());
        map.put("referenceNo", vo.getReferenceNo());
        map.put("createdby", vo.getCreatedby());
        map.put("created", vo.getCreated());
        map.put("updatedby", vo.getUpdatedby());
        map.put("updated", vo.getUpdated());
        return map;
    }
    /**
     * 加载卸货方式
     * @return
     */
    public static List<Map<String,Object>> buildReturnWarehouseinRuleUnloadType(List<WarehouseinRuleUnloadTypeVo> list){
        List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
        if (CollectionUtils.isEmpty(list)) {
            return result;
        }
        Map<String, Object> map = null;
        for (WarehouseinRuleUnloadTypeVo vo : list) {
            map = new HashMap<String, Object>();
            map.put("warehouseCode", vo.getWarehouseCode());
            map.put("warehouseName", vo.getWarehouseName());
            map.put("unloadType", vo.getUnloadType());
            map.put("createdby", vo.getCreatedby());
            map.put("created", vo.getCreated());
            map.put("updatedby", vo.getUpdatedby());
            map.put("updated", vo.getUpdated());
            result.add(map);
        }
        return result;
    }
  
}
